package yuan.zk.watcher;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/11 17:25
 */


public class ZKWatcherGetData {
    public static final String ip = "192.168.190.128:2181";
    public static final int TIME_OUT = 5000;
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(ip, TIME_OUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("connection successfully");
                    latch.countDown();
                }
            }

        });
        latch.await();
    }

    @After
    public void after() throws Exception {
        zooKeeper.close();
    }


    @Test
    public void test2() throws KeeperException, InterruptedException {
        zooKeeper.getData("/watcher1", true, null);
        Thread.sleep(50000);

    }
    @Test
    public void test1() throws KeeperException, InterruptedException {
        zooKeeper.getData("/watcher1",
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("type: " + watchedEvent.getType());
                    }
                }, null);

        Thread.sleep(50000);
    }
    @Test
    public void test3() throws Exception{
        zooKeeper.getData("/watcher1",
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("1");
                        System.out.println("Type: " + watchedEvent.getType());
                        if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                            try {
                                zooKeeper.getData("/watcher1", this, null);
                            } catch (KeeperException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, null);
        zooKeeper.getData("/watcher1",
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("2");
                        System.out.println("Type: " + watchedEvent.getType());
                        if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                            try {
                                zooKeeper.getData("/watcher1", this, null);
                            } catch (KeeperException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, null);


        Thread.sleep(50000);

    }
    @Test
    public void test4() throws Exception{
        zooKeeper.getChildren("/watcher3", true);
        Thread.sleep(50000);

    }
    @Test
    public void test5() throws Exception{
        AtomicInteger atomic = new AtomicInteger(0);
        zooKeeper.getChildren("/watcher3", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println(atomic.getAndIncrement());
                System.out.println("path:" + watchedEvent.getPath());
                System.out.println("type:" + watchedEvent.getType());
                if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {

                    try {
                        zooKeeper.getChildren("/watcher3", this);
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        Thread.sleep(50000);

    }
    @Test
    public void test6() throws Exception{

    }


}
